package com.zhangyujun.bysj.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhangyujun.bysj.common.exception.GuiguException;
import com.zhangyujun.bysj.manager.mapper.SysMenuMapper;
import com.zhangyujun.bysj.manager.mapper.SysRoleMenuMapper;
import com.zhangyujun.bysj.manager.service.SysMenuService;
import com.zhangyujun.bysj.manager.utils.MenuHelper;
import com.zhangyujun.bysj.model.entity.system.SysMenu;
import com.zhangyujun.bysj.model.vo.common.ResultCodeEnum;
import com.zhangyujun.bysj.model.vo.system.SysMenuVo;
import com.zhangyujun.bysj.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    //菜单列表
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> findNodes() {
        //1 查询所有的菜单 返回list集合
        List<SysMenu> sysMenus = sysMenuMapper.findAll();
        if (CollectionUtil.isEmpty(sysMenus)) {
            return null;
        }
        //2 调用工具类里面的方法 把返回list集合封装符合要求的的数据格式
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenus);
        return treeList;
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
        //新添加菜单 把父菜单isHalf半开状态改为1
        updateSysRoleMenu(sysMenu);
    }

    //
    private void updateSysRoleMenu(SysMenu sysMenu) {
      //获取当前菜单的父菜单
      SysMenu parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());
      //判断父菜单是否为空
        if (parentMenu != null) {
            //把父菜单的isHalf状态改为1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
            //递归调用
            updateSysRoleMenu(parentMenu);
        }
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.selectCountById(id);
        //判断，count大于0，包含子菜单
        if (count > 0) {
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.delete(id);
    }

    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        //获取当前登录用户id
        Long userId = AuthContextUtil.get().getId();
        //根据userId查询可以操作的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.findMenusByUserId(userId);
        //封装要求的格式数据 返回
        List<SysMenu> sysMenus = MenuHelper.buildTree(sysMenuList);
        List<SysMenuVo> sysMenuVos = this.buildMenus(sysMenus);

        return sysMenuVos;
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
